package com.winchain.chainbill.repayment.service.impl;

import com.shell.bee.base.utils.SystemUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbDctOverdue;
import com.winchain.chainbill.bill.base.po.CbDctRpmt;
import com.winchain.chainbill.bill.base.service.CbDctOverdueService;
import com.winchain.chainbill.bill.base.service.CbDctRpmtService;
import com.winchain.chainbill.repayment.service.BillDctRpmtService;
import com.winchain.chainbill.repayment.vo.CbDctRpmtVo;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billDctRpmtService")
public class BillDctRpmtServiceImpl implements BillDctRpmtService {
	@Autowired
	private CbDctRpmtService cbDctRpmtService;
	@Autowired
	private CbDctOverdueService cbDctOverdueService;

	public String saveDctRpmt(CbDctRpmtVo dctRpmtVo) {
		CbDctRpmt cbDctRpmt = this.cbDctRpmtService.selectDctRpmtByBizIdAndType(dctRpmtVo.getDctId(),
				dctRpmtVo.getRpmtType());
		if (cbDctRpmt != null) {
			cbDctRpmt.setAmont(dctRpmtVo.getCapital().add(dctRpmtVo.getRpmtInt()));
			cbDctRpmt.setCapital(dctRpmtVo.getCapital());
			cbDctRpmt.setRpmtInt(dctRpmtVo.getRpmtInt());
			if (dctRpmtVo.getOvdInt() != null) {
				cbDctRpmt.setOvdInt(dctRpmtVo.getOvdInt());
			}
			cbDctRpmt.setRpmtDate(dctRpmtVo.getRpmtDate());
			cbDctRpmt.setRpmtState("1");
			this.cbDctRpmtService.updateNotNull(cbDctRpmt);
		} else {
			cbDctRpmt = new CbDctRpmt();
			cbDctRpmt.setRpmtId(SystemUtil.getUUID());
			cbDctRpmt.setBillId(dctRpmtVo.getBillId());
			cbDctRpmt.setBizId(dctRpmtVo.getDctId());
			cbDctRpmt.setRpmtType(dctRpmtVo.getRpmtType());
			cbDctRpmt.setAmont(dctRpmtVo.getCapital().add(dctRpmtVo.getRpmtInt()));
			cbDctRpmt.setCapital(dctRpmtVo.getCapital());
			cbDctRpmt.setRpmtInt(dctRpmtVo.getRpmtInt());
			if (dctRpmtVo.getOvdInt() != null) {
				cbDctRpmt.setOvdInt(dctRpmtVo.getOvdInt());
			}
			cbDctRpmt.setRpmtState("1");
			cbDctRpmt.setRpmtMethod("1");
			cbDctRpmt.setRpmtDate(dctRpmtVo.getRpmtDate());
			cbDctRpmt.setCreateTime(SysDateUtil.getSystemDateTime());
			this.cbDctRpmtService.save(cbDctRpmt);
		}
		CbDctOverdue dctOverdue = this.cbDctOverdueService.selectDctOverdueByBizIdAndType(dctRpmtVo.getDctId(),
				dctRpmtVo.getRpmtType());
		if (dctOverdue != null) {
			dctOverdue.setRpmtDate(dctRpmtVo.getRpmtDate());
			dctOverdue.setOvdInt(dctRpmtVo.getOvdInt());
			dctOverdue.setOvdState("1");
			this.cbDctOverdueService.updateNotNull(dctOverdue);
		}
		return "1";
	}
}
